package pers.lc.testDataGenerator.rule;

import pers.lc.testDataGenerator.enumeration.DataEnumConst;
import pers.lc.testDataGenerator.exception.IllegalColumnTypeException;
import pers.lc.testDataGenerator.exception.NotFindColumnTypeException;

import java.math.BigDecimal;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.util.Random;

/**
 * 范围选取规则
 */
public class ColumnRangeRule {
    private String type;
    private String interval;
    public ColumnRangeRule(){

    }
    public ColumnRangeRule(String type,String interval){
        this.type = type;
        this.interval = interval;
    }

    public String getType() {
        return type;
    }

    public String getInterval() {
        return interval;
    }

    public void setInterval(String interval) {
        this.interval = interval;
    }

    public void setType(String type) {
        this.type = type;
    }
    public static String singleRule1(String from, String to, String type, String formatter){
        Random random = new Random();
        if (type.equals(DataEnumConst.columnTypeEnum.LONG.getValue())){
            return String.valueOf((long)(Long.valueOf(from)+(Long.valueOf(to)-Long.valueOf(from))*random.nextDouble()));
        } else if (type.equals(DataEnumConst.columnTypeEnum.DECIMAL.getValue())) {
            BigDecimal fromDecimal = new BigDecimal(from);
            BigDecimal toDecimal = new BigDecimal(to);
            return fromDecimal.add(toDecimal.subtract(fromDecimal).multiply(BigDecimal.valueOf(random.nextDouble()))).toString();
        } else if (type.equals(DataEnumConst.columnTypeEnum.STRING.getValue())) {
            throw new IllegalColumnTypeException("String字符类型非法");
        } else if (type.equals(DataEnumConst.columnTypeEnum.BOOLEAN.getValue())) {
            throw new IllegalColumnTypeException("Boolean字符类型非法");
        } else if (type.equals(DataEnumConst.columnTypeEnum.LOCAL_DATE.getValue())) {
            LocalDate fromDate = LocalDate.parse(from, DateTimeFormatter.ofPattern(formatter));
            LocalDate toDate = LocalDate.parse(to, DateTimeFormatter.ofPattern(formatter));
            return fromDate.plusDays(random.nextInt(Math.abs((int)(fromDate.toEpochDay()-toDate.toEpochDay())))).format(DateTimeFormatter.ofPattern(formatter));
        } else if (type.equals(DataEnumConst.columnTypeEnum.LOCAL_DATE_TIME.getValue())) {
            LocalDateTime fromDateTime = LocalDateTime.parse(from, DateTimeFormatter.ofPattern(formatter));
            LocalDateTime toDateTime = LocalDateTime.parse(to, DateTimeFormatter.ofPattern(formatter));
            Duration interval = Duration.between(fromDateTime,toDateTime);
            return fromDateTime.plusSeconds((long)(interval.getSeconds()*random.nextDouble())).format(DateTimeFormatter.ofPattern(formatter));
        }else {
            throw new NotFindColumnTypeException("找不到的字段类型："+type);
        }
    }
    public static String multiRule1(String from, String to, String type, String formatter){
        return singleRule1(from,to,type,formatter);
    }
    public static String multiRule2(String from, String to, String type, String formatter,String interval, int count){
        if (type.equals(DataEnumConst.columnTypeEnum.LONG.getValue())){
            Long index = Long.valueOf(interval)*count;
            Long size = Long.valueOf(to)-Long.valueOf(from);
            return String.valueOf(Long.valueOf(from)+(index>size?Math.floorMod(index,size):index));
        } else if (type.equals(DataEnumConst.columnTypeEnum.DECIMAL.getValue())) {
            BigDecimal fromDecimal = new BigDecimal(from);
            BigDecimal toDecimal = new BigDecimal(to);
            BigDecimal intervalDecimal = new BigDecimal(interval);
            BigDecimal countDecimal = new BigDecimal(count);
            BigDecimal index = intervalDecimal.multiply(countDecimal);
            BigDecimal size = toDecimal.subtract(fromDecimal);
            return fromDecimal.add(index.compareTo(size)==1?index.divideAndRemainder(size)[1]:index).toString();
        } else if (type.equals(DataEnumConst.columnTypeEnum.STRING.getValue())) {
            throw new IllegalColumnTypeException("String字符类型非法");
        } else if (type.equals(DataEnumConst.columnTypeEnum.BOOLEAN.getValue())) {
            throw new IllegalColumnTypeException("Boolean字符类型非法");
        } else if (type.equals(DataEnumConst.columnTypeEnum.LOCAL_DATE.getValue())) {
            LocalDate fromDate = LocalDate.parse(from, DateTimeFormatter.ofPattern(formatter));
            LocalDate toDate = LocalDate.parse(to, DateTimeFormatter.ofPattern(formatter));
            Long index = Long.valueOf(interval)*count;
            int size = Period.between(fromDate,toDate).getDays();
            return fromDate.plusDays(index>size?Math.floorMod(index,size):index).format(DateTimeFormatter.ofPattern(formatter));
        } else if (type.equals(DataEnumConst.columnTypeEnum.LOCAL_DATE_TIME.getValue())) {
            LocalDateTime fromDateTime = LocalDateTime.parse(from, DateTimeFormatter.ofPattern(formatter));
            LocalDateTime toDateTime = LocalDateTime.parse(to, DateTimeFormatter.ofPattern(formatter));
            Long index = Long.valueOf(interval)*count;
            Long size = Duration.between(fromDateTime,toDateTime).getSeconds();
            return fromDateTime.plusSeconds(index>size?Math.floorMod(index,size):index).format(DateTimeFormatter.ofPattern(formatter));
        }else {
            throw new NotFindColumnTypeException("找不到的字段类型："+type);
        }
    }
}
